//
//  PayMarkController.swift
//  Unzip
//
//  Created by zhibao on 2024/2/12.
//

import UIKit
@objc class PayMarkController: SecSuperController ,SDCycleScrollViewDelegate{
    
    let payIDs = [
        "com.jieyaya.zhou",
        "com.jieyaya.yue",
        "com.jieyaya.ji",
        "com.jieyaya.year"
    ]
    
    let payUsers =  [
        [ "name": LocalizedString("136****876"),
          "detail": LocalizedString("购买了连续包周会员"),
          "time": LocalizedString("2分钟前")
        ],
        [ "name": LocalizedString("188****124"),
          "detail": LocalizedString("购买了连续包月会员"),
          "time": LocalizedString("4分钟前")
        ],
        [ "name": LocalizedString("156****873"),
          "detail": LocalizedString("购买了连续包周会员"),
          "time": LocalizedString("10分钟前")
        ],
        [ "name": LocalizedString("90W***QSD"),
          "detail": LocalizedString("购买了连续包周会员"),
          "time": LocalizedString("1小时前")
        ],
        [ "name": LocalizedString("LOI***Q23"),
          "detail": LocalizedString("购买了连续包年会员"),
          "time": LocalizedString("5小时前")
        ],
        [ "name": LocalizedString("QAZ***W13"),
          "detail": LocalizedString("购买了连续包周会员"),
          "time": LocalizedString("6小时前")
        ],
        [ "name": LocalizedString("HGY***827"),
          "detail": LocalizedString("购买了连续包月会员"),
          "time": LocalizedString("7小时前")
        ],
        [ "name": LocalizedString("ZY3***PL9"),
          "detail": LocalizedString("购买了连续包月会员"),
          "time": LocalizedString("12小时前")
        ],
        [ "name": LocalizedString("IY7***L09"),
          "detail": LocalizedString("购买了连续包周会员"),
          "time": LocalizedString("昨天")
        ],
        [ "name": LocalizedString("TSD***333"),
          "detail": LocalizedString("购买了连续包周会员"),
          "time": LocalizedString("昨天")
        ]
    ]
    
    let pinglunArray = [
        [
            "name": LocalizedString("潘恩依"),
            "type": LocalizedString("连续包月"),
            "zhiye": LocalizedString("（大学生）"),
            "content": LocalizedString("工作上经常需要使用此类软件，这个软件很方便，速度很快，点赞👍")
        ],
        [
            "name": LocalizedString("王生安"),
            "type": LocalizedString("连续包年"),
            "zhiye": LocalizedString("（个体户）"),
            "content": LocalizedString("朋友推荐的，使用非常流畅，响应速度快，无广告，功能强大！")
        ],
        [
            "name": LocalizedString("周卓浩"),
            "type": LocalizedString("连续包周"),
            "zhiye": LocalizedString("（律师）"),
            "content": LocalizedString("处理速度很快，界面很友好，和其他APP很不一样，方便快捷！")
        ],
        [
            "name": LocalizedString("刘静静"),
            "type": LocalizedString("连续包月"),
            "zhiye": LocalizedString("（白领）"),
            "content": LocalizedString("好用，推荐给身边好多朋友，都说非常好！")
        ],
        [
            "name": LocalizedString("陈晓倩"),
            "type": LocalizedString("连续包年"),
            "zhiye": LocalizedString("（白领）"),
            "content": LocalizedString("真的没想到还有无广告的此类APP，用了它，工作效率大大提升，非常喜欢！")
        ]
    ]
    
    var payInfoBtns = [UIButton]()
    var payBtn = UIButton(type: .custom)
    var selectIndex = 0
    let agreeBtn = UIButton(type: .custom)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initView()
        
        let twoBtn = UIButton(type: .custom)
        twoBtn.frame = CGRectMake(0, 0, pixw(p:80), pixw(p:60))
        twoBtn.addTarget(self, action: #selector(repayClick), for: .touchUpInside)
        twoBtn.setTitle(LocalizedString("恢复购买"), for: .normal)
        twoBtn.setTitleColor(MYOrange, for: .normal)
        twoBtn.titleLabel?.font = MY_SCALE_RegularFONT(13)
        let item = UIBarButtonItem(customView: twoBtn)
        self.gk_navRightBarButtonItems = [item]
    }
    
    @objc fileprivate func repayClick(){
        QMUITips.showInfo(LocalizedString("暂无可恢复项！"))
    }
    
    fileprivate func initView(){
        let contentScrollview = UIScrollView(frame: CGRect(x: 0, y: navigationFullHeight(), width: SCREEN_WIDTH, height: SCREEN_HEIGHT - navigationFullHeight() - pixw(p: 45) -  pixw(p: 12) - safeDistanceBottom()))
        contentScrollview.backgroundColor = UIColor.clear
        contentScrollview.showsVerticalScrollIndicator = false
        self.view.addSubview(contentScrollview)
        
//        let userCycleView = SDCycleScrollView(frame: CGRect(x: pixw(p: 15), y: pixw(p: 15), width: SCREEN_WIDTH - pixw(p: 30), height: pixw(p: 23)), imageNamesGroup: payUsers)
        let userCycleView = SDCycleScrollView(frame: CGRect(x: pixw(p: 15), y: pixw(p: 15), width: SCREEN_WIDTH - pixw(p: 30), height: pixw(p: 23)), imageNamesGroup: ["s","s","s","s","s","s","s","s","s","s"])
        userCycleView?.tag = 1000
        userCycleView?.backgroundColor = MYColor.color(withHexString: "25282D", alpha: 0.9)
        userCycleView?.layer.cornerRadius =  pixw(p: 5)
        userCycleView?.autoScroll = true
       
        userCycleView?.autoScrollTimeInterval = 2
        userCycleView?.showPageControl = false
        contentScrollview.addSubview(userCycleView!)
        userCycleView?.delegate = self
        userCycleView?.scrollDirection = .vertical
        userCycleView?.snp.makeConstraints({ make in
            make.left.top.equalTo(pixw(p: 15))
            make.right.equalTo(self.view).offset(-pixw(p: 15))
            make.height.equalTo(pixw(p: 23))
        })
        
        var tempView = UIView()
        tempView = userCycleView!
        for i in 0 ..< payIDs.count {
            let btn = UIButton(type: .custom)
            btn.frame = CGRect(x:  pixw(p: 15), y: tempView.bottom +  pixw(p: 15), width: SCREEN_WIDTH -  pixw(p: 30), height:  pixw(p: 56))
            btn.layer.cornerRadius =  pixw(p: 15)
            btn.tag = 1000 + i
            btn.addTarget(self, action: #selector(btnClick(sender:)), for: .touchUpInside)
            tempView = btn
            payInfoBtns.append(btn)
            contentScrollview.addSubview(btn)
            
            let titleLabel = UILabel()
            titleLabel.font = MY_SCALE_SemiboldFONT(13)
            titleLabel.textColor = UIColor.white
            btn.addSubview(titleLabel)
            titleLabel.snp.makeConstraints { make in
                make.left.equalTo( pixw(p: 15))
                make.width.equalTo( pixw(p: 200))
                make.top.equalTo( pixw(p: 8))
            }
            
            let moneyLabel = UILabel()
            moneyLabel.font = MY_SCALE_SemiboldFONT(18)
            moneyLabel.textColor = UIColor.white
            btn.addSubview(moneyLabel)
            moneyLabel.snp.makeConstraints { make in
                make.right.equalTo(pixw(p: -15))
                make.centerY.equalTo(btn)
            }
            
            let detailLabel = UILabel()
            detailLabel.font = MY_SCALE_RegularFONT(11)
            detailLabel.textColor = UIColor.white
            detailLabel.numberOfLines = 0
            detailLabel.lineBreakMode = .byWordWrapping
            btn.addSubview(detailLabel)
            detailLabel.snp.makeConstraints { make in
                make.left.equalTo(titleLabel)
                make.right.equalTo(moneyLabel.snp.left).offset(20)
                make.top.equalTo(titleLabel.snp.bottom).offset(4)
            }
            if IAPShare.sharedHelper().iap.products.count > 0 {
                let products = IAPShare.sharedHelper().iap.products as! [SKProduct]
                for product in products {
                    if product.productIdentifier == payIDs[i] {
                        moneyLabel.text = IAPShare.sharedHelper().iap.getLocalePrice(product)
                        titleLabel.text = product.localizedTitle
                        detailLabel.text = product.localizedDescription
                        DLog(message: "detail ==\(product.localizedTitle)")
                        DLog(message: "detail ==\(product.localizedDescription)")
                        if MYGlobal.share().wechatFlag != 1 {
                            let detailArray = product.localizedDescription.components(separatedBy: "-")
                            if detailArray.count > 1 {
                                detailLabel.text = detailArray.first
                            }
                        }
                    }
                }
            }else{
                if i == 0 {
                    titleLabel.text = "连续包周"
                    detailLabel.text = "7天-到期自动续订,可随时取消。"
                    moneyLabel.text = "￥12.00"
                }else if i == 1 {
                    titleLabel.text = "连续包月"
                    detailLabel.text = "1个月-到期自动续订,可随时取消。"
                    moneyLabel.text = "￥18.00"
                }else if i == 2 {
                    titleLabel.text = "连续包季"
                    detailLabel.text = "3个月-到期自动续订,可随时取消。"
                    moneyLabel.text = "￥38.00"
                }else if i == 3 {
                    titleLabel.text = "连续包年"
                    detailLabel.text = "1年-到期自动续订,可随时取消。"
                    moneyLabel.text = "￥98.00"
                }
            }
            
            
            if  i == 0 {
                Helper.setDiferentColors([MYColor.color(withHexString: "2666e4", alpha: 0.6).cgColor,MYColor.color(withHexString: "2666e4", alpha: 0.3).cgColor], with: btn, withFlag: .directionLeft)
                let vip = DWKeychainTool.getItemForKey("vip")
                if (vip == nil && MYGlobal.share().wechatFlag != 1){
                    let zheLabel = UILabel()
                    zheLabel.textColor = MYColor.color(withHexString: "FFC5C5")
                    zheLabel.font = MY_SCALE_RegularFONT(12)
                    btn.addSubview(zheLabel)
                    zheLabel.snp.makeConstraints { make in
                        make.right.equalTo(moneyLabel.snp.left).offset(-10)
                        make.centerY.equalTo(moneyLabel)
                    }
                    
                    let text = "\(String(describing: moneyLabel.text!))"
                    zheLabel.attributedText = NSAttributedString(string: text,
                                                                      attributes: [NSAttributedString.Key.strikethroughStyle: NSUnderlineStyle.single.rawValue])
                    moneyLabel.text = LocalizedString("首次优惠")
                    moneyLabel.numberOfLines = 0
                    moneyLabel.lineBreakMode = .byWordWrapping
                    moneyLabel.font = MY_SCALE_MediumFONT(11)
                }
            }else{
                Helper.setDiferentColors([MYColor.color(withHexString: "666666", alpha: 0.6).cgColor,MYColor.color(withHexString: "666666", alpha: 0.3).cgColor], with: btn, withFlag: .directionLeft)
            }
        }
        
        //会员特权
        let vipClabel = UILabel()
        vipClabel.text = LocalizedString("Vip特权")
        vipClabel.font = MY_SCALE_BoldFont(15)
        vipClabel.textColor = UIColor.white
        contentScrollview.addSubview(vipClabel)
        vipClabel.snp.makeConstraints { make in
            make.left.equalTo(tempView)
            make.top.equalTo(tempView.snp.bottom).offset(25)
        }
        
        tempView = vipClabel
        
        let vipQXArray = [
            [
                "title":LocalizedString("支持zip/rar/7z等格式"),
                "img":"doc.zipper"
            ],
            [
                "title":LocalizedString("不限次数压缩"),
                "img":"rectangle.compress.vertical"
            ],
            [
                "title":LocalizedString("不限次数解压"),
                "img":"mount"
            ],
            [
                "title":LocalizedString("设置多重压缩密码"),
                "img":"lock"
            ],
            [
                "title":LocalizedString("更多传输方式"),
                "img":"transmission"
            ],
        ]
        
        for vipDQ in vipQXArray {
            let vipContentlabel = UILabel()
            vipContentlabel.text = vipDQ["title"]
            vipContentlabel.numberOfLines = 0
            vipContentlabel.lineBreakMode = .byWordWrapping
            vipContentlabel.font = MY_SCALE_MediumFONT(13)
            vipContentlabel.textColor = UIColor.white
            contentScrollview.addSubview(vipContentlabel)
            vipContentlabel.snp.makeConstraints { make in
                make.left.equalTo(pixw(p:60))
                make.right.equalTo(tempView)
                make.top.equalTo(tempView.snp.bottom).offset(pixw(p:10))
            }
            tempView = vipContentlabel
            
            let vipImgView = UIImageView(image: UIImage(systemName: vipDQ["img"]!))
            vipImgView.tintColor = MYOrange
            contentScrollview.addSubview(vipImgView)
            vipImgView.snp.makeConstraints { make in
                make.centerY.equalTo(vipContentlabel)
                make.right.equalTo(vipContentlabel.snp.left).offset(pixw(p:-10))
            }
        }
        
        let ctipLabel = UILabel()
        ctipLabel.font = MY_SCALE_RegularFONT(11)
        ctipLabel.textColor = UIColor.white
        ctipLabel.text = LocalizedString("自动续费服务说明")
        contentScrollview.addSubview(ctipLabel)
        ctipLabel.snp.makeConstraints { make in
            make.top.equalTo(tempView.snp.bottom).offset(pixw(p: 25))
            make.left.equalTo(pixw(p: 15))
            make.right.equalTo(pixw(p: -15))
        }
        
        let tipLabel = YYLabel()
        contentScrollview.addSubview(tipLabel)
        let string = NSMutableString()
        string.append(LocalizedString("付款将在确认购买时从iTunes帐户中扣除。除非在当前期间结束前至少24小时关闭自动续订，否则订阅会自动续订。帐户将在当前期间结束前24小时内收取续订费用，并确定续订费用。用户可以在AppStore的账户设置中管理他们的订阅，并关闭自动续订。当用户通过免费试用注册订阅时，他们的订阅会立即开始，但在免费试用期结束之前不会向他们收费。开通前请阅读"))
        string.append(LocalizedString("《用户协议》"))
        string.append("、")
        string.append(LocalizedString("《隐私协议》"))
        string.append("、")
        string.append(LocalizedString("《订阅协议》"))
        string.append("。")
        let yhrange = string.range(of: LocalizedString("《用户协议》"))
        let ysrange = string.range(of: LocalizedString("《隐私协议》"))
        let dyrange = string.range(of: LocalizedString("《订阅协议》"))
        let atti = NSMutableAttributedString(string: string as String)
        atti.yy_font = MY_SCALE_RegularFONT(11)
        atti.yy_color = UIColor.white
        atti.yy_lineSpacing = 4
        tipLabel.numberOfLines = 0
        tipLabel.preferredMaxLayoutWidth = SCREEN_WIDTH - pixw(p: 40)
        tipLabel.lineBreakMode = .byWordWrapping
        tipLabel.snp.makeConstraints { make in
            make.left.equalTo(pixw(p: 20))
            make.right.equalTo(self.view).offset(pixw(p: -20))
            make.top.equalTo(ctipLabel.snp.bottom).offset(pixw(p: 10))
        }
        
        atti.yy_setTextHighlight(yhrange, color: MYColor.color(withHexString: "#F1C565"), backgroundColor: UIColor.clear) { containerView, text, range, rect in
            let webViewContoller = WebViewController(title: LocalizedString("用户协议"), withUrl: "https://docs.qq.com/doc/p/5943386fa75c7dda0fdcda4f15cc2019a7990fe7")
            self.navigationController?.pushViewController(webViewContoller, animated: true)
        }
        
        atti.yy_setTextHighlight(ysrange, color: MYColor.color(withHexString: "#F1C565"), backgroundColor: UIColor.clear) { containerView, text, range, rect in
            let webViewContoller = WebViewController(title: LocalizedString("隐私协议"), withUrl: "https://docs.qq.com/doc/p/463ef38ab26f73daa0a015564739388772484e4e")
            self.navigationController?.pushViewController(webViewContoller, animated: true)
        }
        
        atti.yy_setTextHighlight(dyrange, color: MYColor.color(withHexString: "#F1C565"), backgroundColor: UIColor.clear) { containerView, text, range, rect in
            let webViewContoller = WebViewController(title: LocalizedString("订阅协议"), withUrl: "https://docs.qq.com/doc/p/eb4b8a1b2bca62a22d4597d64266414be7597fb4")
            self.navigationController?.pushViewController(webViewContoller, animated: true)
        }
        
        tipLabel.attributedText = atti
        
        tempView = tipLabel
        
        
        let pinglunCycleView = SDCycleScrollView(frame: CGRectMake(pixw(p: 15), tempView.bottom + pixw(p: 20), SCREEN_WIDTH - pixw(p: 30), pixw(p: 115)), imageNamesGroup: ["s","s","s","s","s"])
        pinglunCycleView?.backgroundColor = MYColor.color(withHexString: "#202224")
        pinglunCycleView?.layer.cornerRadius = pixw(p: 20)
        pinglunCycleView?.autoScroll = true
        pinglunCycleView?.autoScrollTimeInterval = 4
        pinglunCycleView?.pageControlStyle = SDCycleScrollViewPageContolStyleClassic
        pinglunCycleView?.pageControlBottomOffset = 1
        contentScrollview.addSubview(pinglunCycleView!)
        
        pinglunCycleView?.snp.makeConstraints({ make in
            make.left.equalTo(pixw(p: 15))
            make.top.equalTo(tempView.snp.bottom).offset(pixw(p: 20))
            make.right.equalTo(self.view).offset(-pixw(p: 15))
            make.height.equalTo(pixw(p: 115))
            make.bottom.equalTo(contentScrollview.snp.bottom).offset(pixw(p: -100))
        })
        pinglunCycleView?.delegate = self
        pinglunCycleView?.scrollDirection = .horizontal
        
        tempView = pinglunCycleView!
        
        payBtn.layer.cornerRadius = pixw(p: 22.5)
        payBtn.backgroundColor = MYBlue
        self.view.addSubview(payBtn)
        payBtn.setTitle(LocalizedString("立即开通"), for: .normal)
        payBtn.addTarget(self, action: #selector(toPay), for: .touchUpInside)
        payBtn.titleLabel?.font = MY_SCALE_SemiboldFONT(16)
        payBtn.setTitleColor(UIColor.white, for: .normal)
        payBtn.snp.makeConstraints { make in
            make.left.equalTo(pixw(p: 15))
            make.right.equalTo(self.view).offset(pixw(p: -15))
            make.height.equalTo(pixw(p: 45))
            make.bottom.equalTo(self.view.snp.bottom).offset(pixw(p: -40) - safeDistanceBottom());
        }
        
       
        agreeBtn.layer.cornerRadius = pixw(p: 3)
        agreeBtn.backgroundColor = MYBlue
        agreeBtn.layer.borderWidth = pixw(p: 0.5)
        agreeBtn.layer.borderColor = UIColor.white.cgColor
        self.view.addSubview(agreeBtn)
        agreeBtn.titleLabel?.font = MY_SCALE_BoldFont(20)
        agreeBtn.setTitleColor(UIColor.white, for: .normal)
        agreeBtn.setTitle("√", for: .normal)
        agreeBtn.tag = 101
        agreeBtn.titleLabel?.font = MY_SCALE_SemiboldFONT(16)
        agreeBtn.setTitleColor(UIColor.white, for: .normal)
        agreeBtn.addTarget(self, action: #selector(agreeClick(sender:)), for: .touchUpInside)
        agreeBtn.snp.makeConstraints { make in
            make.left.equalTo(payBtn).offset(pixw(p: 20))
            make.top.equalTo(payBtn.snp.bottom).offset(pixw(p:20))
            make.width.height.equalTo(pixw(p: 20))
        }
        
        let agreeLabel = YYLabel()
        self.view.addSubview(agreeLabel)
        let string1 = NSMutableString()
        string1.append(LocalizedString("点击同意"))
        string1.append(LocalizedString("《用户协议》"))
        string1.append("、")
        string1.append(LocalizedString("《隐私协议》"))
        string1.append("、")
        string1.append(LocalizedString("《订阅协议》"))
        string1.append("。")
        let yhrange1 = string1.range(of: LocalizedString("《用户协议》"))
        let ysrange1 = string1.range(of: LocalizedString("《隐私协议》"))
        let dyrange1 = string1.range(of: LocalizedString("《订阅协议》"))
        let atti1 = NSMutableAttributedString(string: string1 as String)
        atti1.yy_font = MY_SCALE_RegularFONT(10)
        atti1.yy_color = UIColor.white
        atti1.yy_lineSpacing = 4
        agreeLabel.numberOfLines = 0
        agreeLabel.preferredMaxLayoutWidth = SCREEN_WIDTH - pixw(p: 40 + 20)
        agreeLabel.lineBreakMode = .byWordWrapping
        agreeLabel.snp.makeConstraints { make in
            make.left.equalTo(agreeBtn.snp.right).offset(pixw(p: 4))
            make.right.equalTo(self.view).offset(pixw(p: -20))
            make.centerY.equalTo(agreeBtn)
        }
        
        atti1.yy_setTextHighlight(yhrange1, color: MYColor.color(withHexString: "#F1C565"), backgroundColor: UIColor.clear) { containerView, text, range, rect in
            let webViewContoller = WebViewController(title: LocalizedString("用户协议"), withUrl: "https://docs.qq.com/doc/p/5943386fa75c7dda0fdcda4f15cc2019a7990fe7")
            self.navigationController?.pushViewController(webViewContoller, animated: true)
        }
        
        atti1.yy_setTextHighlight(ysrange1, color: MYColor.color(withHexString: "#F1C565"), backgroundColor: UIColor.clear) { containerView, text, range, rect in
            let webViewContoller = WebViewController(title: LocalizedString("隐私协议"), withUrl: "https://docs.qq.com/doc/p/463ef38ab26f73daa0a015564739388772484e4e")
            self.navigationController?.pushViewController(webViewContoller, animated: true)
        }
             
        atti1.yy_setTextHighlight(dyrange1, color: MYColor.color(withHexString: "#F1C565"), backgroundColor: UIColor.clear) { containerView, text, range, rect in
            let webViewContoller = WebViewController(title: LocalizedString("订阅协议"), withUrl: "https://docs.qq.com/doc/p/eb4b8a1b2bca62a22d4597d64266414be7597fb4")
            self.navigationController?.pushViewController(webViewContoller, animated: true)
        }
        
        agreeLabel.attributedText = atti1
    }
    
    @objc fileprivate func agreeClick(sender:UIButton){
        if sender.tag == 101 {
            //不同意
            sender.tag = 100
            sender.setTitle("", for: .normal)
            sender.backgroundColor = UIColor.clear
        }else{
            sender.tag = 101
            sender.setTitle("√", for: .normal)
            sender.backgroundColor = MYBlue
        }
        
    }
    
    @objc fileprivate func btnClick(sender:UIButton){
        for btn in payInfoBtns {
            Helper.setDiferentColors([MYColor.color(withHexString: "666666", alpha: 0.6).cgColor,MYColor.color(withHexString: "666666", alpha: 0.3).cgColor], with: btn, withFlag: .directionLeft)
        }
        Helper.setDiferentColors([MYColor.color(withHexString: "2666e4", alpha: 0.6).cgColor,MYColor.color(withHexString: "2666e4", alpha: 0.3).cgColor], with: sender, withFlag: .directionLeft)
        selectIndex = sender.tag - 1000
    }
    
    @objc fileprivate func toPay(){
        if agreeBtn.tag != 101 {
            QMUITips.showError("点击同意《用户协议》、《隐私协议》、《订阅协议》。")
            return
        }
        let payID = payIDs[selectIndex]
        QMUITips.showLoading(LocalizedString("...购买中..."), detailText: LocalizedString("...查询信息中..."), in: self.view)
        IAPShare.sharedHelper().iap.requestProducts { request, response in
            if response?.products.count ?? 0 > 0 {
                var product:SKProduct?
                for pro in response!.products{
                    if pro.productIdentifier == payID {
                        product = pro
                        break
                    }
                }
                
                if let tempPro = product {
                    IAPShare.sharedHelper().iap.buyProduct(tempPro) { trans in
                        if((trans?.error) != nil){
                            QMUITips.hideAllTips()
                            QMUITips.showError(LocalizedString("...购买失败..."), detailText: LocalizedString("...请稍后再试..."))
                        }else if trans?.transactionState == .purchased{
                            
                            //交易验证
                            guard let recepitURL = Bundle.main.appStoreReceiptURL else { return }
                            let receipt = try? Data(contentsOf: recepitURL)
                            if(receipt == nil){
                                QMUITips.hideAllTips()
                                QMUITips.showError(LocalizedString("...购买失败..."), detailText: LocalizedString("...请稍后再试..."))
                                return
                            }
                            
                            var time = Helper.getTime(.zhou)
                            if payID == "com.jieyaya.yue" {
                                time = Helper.getTime(.yue)
                            }else if payID == "com.jieyaya.ji" {
                                time = Helper.getTime(.ji)
                            }else if payID == "com.jieyaya.year" {
                                time = Helper.getTime(.nian)
                            }else if payID == "com.jieyaya.yongjiu" {
                                time = Helper.getTime(.yongJiu)
                            }
                            QMUITips.hideAllTips()
                            MobClick.event("dingyue", attributes: ["type":"goumai","id":payID,"quantity":IAPShare.sharedHelper().iap.getLocalePrice(product) ?? "0"])
                            QMUITips.showSucceed(LocalizedString("...购买成功..."), detailText: LocalizedString("...恭喜您成为Vip会员..."))
                            DWKeychainTool.saveItem(payID, key: "id")
                            DWKeychainTool.saveItem(payID, key: "id")
                            MYGlobal.share().vip_expiry = time!
                            self.navigationController?.popViewController(animated: true)
                            
                            //处理
                        }
                    }
                }else{
                    QMUITips.hideAllTips()
                    QMUITips.showError(LocalizedString("...购买失败..."), detailText: LocalizedString("...请稍后再试..."))
                }
                
            }else{
                DispatchQueue.main.async {
                    QMUITips.hideAllTips()
                    QMUITips.showError(LocalizedString("...购买失败..."), detailText: LocalizedString("...请稍后再试..."))
                }
            }
        }
    }
}

extension PayMarkController{
    func customCollectionViewCellClass(for view: SDCycleScrollView!) -> AnyClass! {
        return UICollectionViewCell.self
    }
    
    func setupCustomCell(_ cell: UICollectionViewCell!, for index: Int, cycleScrollView view: SDCycleScrollView!) {
        for subView in cell.contentView.subviews {
            if subView.tag == 1000 {
                subView.removeFromSuperview()
            }
        }
        DLog(message: "view.tag ==\(view.tag) index==\(index)")
        
        if view.tag == 1000 {
            let headImgView = UIImageView(image: UIImage(named: "pay_center_laba"))
            headImgView.tag = 1000
            cell.contentView.addSubview(headImgView)
            headImgView.snp.makeConstraints { make in
                make.left.equalTo(pixw(p: 10));
                make.centerY.equalTo(cell);
                make.width.height.equalTo(pixw(p: 15));
            }
            
            let nameLabel = UILabel()
            nameLabel.font = MY_SCALE_SemiboldFONT(10)
            nameLabel.textColor = MYColor.color(withHexString: "#B78D28")
            nameLabel.tag = 1000
            nameLabel.text = payUsers[index]["name"]
            cell.contentView.addSubview(nameLabel)
            nameLabel.snp.makeConstraints { make in
                make.centerY.equalTo(headImgView)
                make.left.equalTo(headImgView.snp.right).offset(pixw(p: 10))
            }
            
            let timeLabel = UILabel()
            timeLabel.font = MY_SCALE_SemiboldFONT(10)
            timeLabel.textColor = MYColor.color(withHexString: "#B3B3B3")
            timeLabel.tag = 1000
            timeLabel.text = payUsers[index]["time"]
            cell.contentView.addSubview(timeLabel)
            timeLabel.snp.makeConstraints { make in
                make.centerY.equalTo(headImgView)
                make.right.equalTo(cell).offset(pixw(p: -15))
            }
            
            let detailLabel = UILabel()
            detailLabel.font = MY_SCALE_SemiboldFONT(10)
            detailLabel.textColor = MYColor.color(withHexString: "#B3B3B3")
            detailLabel.tag = 1000
            detailLabel.text = payUsers[index]["detail"]
            cell.contentView.addSubview(detailLabel)
            detailLabel.snp.makeConstraints { make in
                make.centerY.equalTo(headImgView)
                make.left.equalTo(nameLabel.snp.right).offset(4)
                make.right.equalTo(timeLabel.snp.left).offset(-4)
            }
            
           
        }else{
            let headImgView = UIImageView(image: UIImage(named: "pay_center_head"))
            headImgView.tag = 1000
            cell.contentView.addSubview(headImgView)
            headImgView.snp.makeConstraints { make in
                make.left.equalTo(pixw(p: 15))
                make.top.equalTo(pixw(p: 10))
                make.width.height.equalTo(pixw(p: 30))
            }
            
            let nameLabel = UILabel()
            nameLabel.font = MY_SCALE_SemiboldFONT(12)
            nameLabel.textColor = UIColor.white
            nameLabel.tag = 1000
            nameLabel.text = pinglunArray[index]["name"]
            cell.contentView.addSubview(nameLabel)
            nameLabel.snp.makeConstraints { make in
                make.top.equalTo(headImgView.snp.top).offset(pixw(p: 2))
                make.left.equalTo(headImgView.snp.right).offset(pixw(p: 7))
            }
            
            let gzLabel = UILabel()
            gzLabel.font = MY_SCALE_RegularFONT(10)
            gzLabel.textColor = UIColor.white
            gzLabel.tag = 1000
            gzLabel.text = pinglunArray[index]["zhiye"]
            cell.contentView.addSubview(gzLabel)
            gzLabel.snp.makeConstraints { make in
                make.centerY.equalTo(nameLabel)
                make.left.equalTo(nameLabel.snp.right)
            }
            
            let xingImgView = UIImageView(image: UIImage(named: "pay_center_xing"))
            xingImgView.tag = 1000
            cell.contentView.addSubview(xingImgView)
            xingImgView.snp.makeConstraints { make in
                make.left.equalTo(nameLabel)
                make.top.equalTo(nameLabel.snp.bottom).offset(pixw(p: 3))
                make.width.equalTo(pixw(p: 47))
                make.height.equalTo(pixw(p: 7))
            }
            
            let typeLabel = UILabel()
            typeLabel.font = MY_SCALE_RegularFONT(12)
            typeLabel.textColor = UIColor.white
            typeLabel.tag = 1000
            typeLabel.text = pinglunArray[index]["type"]
            cell.contentView.addSubview(typeLabel)
            typeLabel.snp.makeConstraints { make in
                make.right.equalTo(cell).offset(pixw(p: -15))
                make.centerY.equalTo(headImgView)
            }
            
            let typeImgView = UIImageView(image: UIImage(named: "pay_center_vip"))
            typeImgView.tag = 1000
            cell.contentView.addSubview(typeImgView)
            typeImgView.snp.makeConstraints { make in
                make.right.equalTo(typeLabel.snp.left).offset(pixw(p: -5))
                make.centerY.equalTo(headImgView)
            }
            
            let contentLabel = UILabel()
            contentLabel.font = MY_SCALE_RegularFONT(12)
            contentLabel.textColor = MYOrange
            contentLabel.tag = 1000
            contentLabel.text = pinglunArray[index]["content"]
            contentLabel.numberOfLines = 2
            contentLabel.lineBreakMode = .byWordWrapping
            contentLabel.textAlignment = .center
            cell.contentView.addSubview(contentLabel)
            contentLabel.snp.makeConstraints { make in
                make.top.equalTo(headImgView.snp.bottom).offset(pixw(p: 12))
                make.left.equalTo(pixw(p: 15))
                make.right.equalTo(cell).offset(pixw(p: -15))
            }
        }
    }
}
